/* -*-mode:c++; coding:utf-8 */

/* 参考自ACE中Atomic_Op的实现 */

#ifndef _KB_ATOMIC_HPP_
#define _KB_ATOMIC_HPP_

#include <kb/config.hpp>

__BEGIN_KB_NAMESPACE__

template <typename T, typename Mutex>
class atomic {
public:
	typedef Mutex mutex_type;
	typedef T value_type;

private:
	typedef atomic<T,Mutex> this_type;
	
public:
	/// Initialize @c value_ to 0.
	atomic ();

	/// Initialize @c value_ to c.
	atomic (const value_type &p_value);

	/// Manage copying...
	atomic (const this_type &rhs);

	/// Atomically assign rhs to @c value_.
	this_type &operator= (const value_type &rhs);

	/// Atomically assign <rhs> to @c value_.
	this_type &operator= (const this_type &rhs);

	/// Atomically pre-increment @c value_.
	value_type operator++ (void);

	/// Atomically post-increment @c value_.
	value_type operator++ (int);

	/// Atomically increment @c value_ by rhs.
	value_type operator+= (const value_type &rhs);

	/// Atomically pre-decrement @c value_.
	value_type operator-- ();

	/// Atomically post-decrement @c value_.
	value_type operator-- (int);

	/// Atomically decrement @c value_ by rhs.
	value_type operator-= (const value_type &rhs);

	/// Atomically compare @c value_ with rhs.
	bool operator== (const value_type &rhs) const;

	/// Atomically compare @c value_ with rhs.
	bool operator!= (const value_type &rhs) const;

	/// Atomically check if @c value_ greater than or equal to rhs.
	bool operator>= (const value_type &rhs) const;

	/// Atomically check if @c value_ greater than rhs.
	bool operator> (const value_type &rhs) const;

	/// Atomically check if @c value_ less than or equal to rhs.
	bool operator<= (const value_type &rhs) const;

	/// Atomically check if @c value_ less than rhs.
	bool operator< (const value_type &rhs) const;

	/// Explicitly return @c value_.
	value_type value (void) const;

	/// Dump the state of an object.
	void dump (void) const;

private:
	value_type m_value;
	mutex_type m_mutex;
};

template <>
class atomic <int, null_mutex> {
};

template <>
class atomic <long, null_mutex> {
};
	

__END_KB_NAMESPACE__
	

#endif	// _KB_ATOMIC_HPP_
